#include "doubleLinkedList.h"

ND *createList()
{
    ND *head = (ND *)malloc(sizeof(head));
    ERROR_CHECK(NULL, head, "malloc");
    head->Precursor_node = NULL;
    head->successor_node = NULL;
    return head;
}

void insertNodeHead(ND *head, int val)
{
    if (NULL == head)
    {
        puts("invalid head NULL");
        exit(1);
    }
    ND *new_node = (ND *)malloc(sizeof(ND));
    ERROR_CHECK(NULL, new_node, "malloc_new_node_head");
    new_node->data = val;
    new_node->Precursor_node = head;
    new_node->successor_node = head->successor_node;

    if (head->successor_node)
    {
        head->successor_node->Precursor_node = new_node;
    }
    head->successor_node = new_node;
}
void insertNodeTail(ND *head, int val)
{
    if (NULL == head)
    {
        puts("invalid head NULL");
        exit(1);
    }
    ND *new_node = (ND *)malloc(sizeof(ND));
    ERROR_CHECK(NULL, new_node, "malloc_new_node_tail");
    new_node->data = val;
    while (head->successor_node)
        head = head->successor_node;
    new_node->successor_node = head->successor_node;
    new_node->Precursor_node = head;
    head->successor_node = new_node;
}
void sendMsg(ND *head, int fd, char *buf, int len)
{
    if (NULL == head)
    {
        puts("invalid head NULL");
        exit(1);
    }
    char buf_new[4096];
    sprintf(buf_new, "%s<msg from %d>", buf, fd);
    printf("fd%d\n", fd);
    while (head = head->successor_node)
    {
        if (head->data == fd)
        {
            continue;
        }
        char *p = " ";
        char *word = strtok(buf, p);
        if (strncmp(buf, "->", 2) == 0)
        {
            word = strtok(NULL, p);
            printf("word:%s\n", word);
            int fd_dest = atoi(word);
            write(fd_dest, buf_new, len + 13);
            memset(buf, 0, 4096);
            return;
        }
        write(head->data, buf_new, len + 13);
        memset(buf, 0, 4096);
    }
    puts("send over!");
}

ND *selectVal(ND *head, int val)
{
    if (NULL == head)
    {
        puts("invalid head NULL");
        exit(1);
    }
    while (head = head->successor_node)
    {
        if (head->data == val)
            return head;
    }

    return NULL;
}
void changeVal(ND *head, int srcVal, int destVal)
{
    if (NULL == head)
    {
        puts("invalid head NULL");
        exit(1);
    }
    ND *temp;
    while (temp = selectVal(head, srcVal))
    {
        if (destVal == temp->data)
        {
            break;
        }
        temp->data = destVal;
    }
}
void delVal(ND *head, int val)
{
    if (NULL == head)
    {
        puts("invalid head NULL");
        exit(1);
    }
    ND *temp1;
    while (temp1 = selectVal(head, val))
    {
        temp1->Precursor_node->successor_node = temp1->successor_node;
        if (temp1->successor_node != NULL)
        {
            temp1->successor_node->Precursor_node = temp1->Precursor_node;
        }

        free(temp1);
    }
}
int listLen(ND *head)
{
    if (NULL == head)
    {
        puts("invalid head NULL");
        exit(1);
    }
    int ret;
    while (head = head->successor_node)
    {
        ret++;
    }
    return ret;
    // 递归求长度
    //  if (head->successor_node == NULL)
    //  {
    //     return 0;
    //  }
    //  return 1 + listLen(head->successor_node);
}
int isEmpty(ND *head)
{
    if (NULL == head)
    {
        puts("invalid head NULL");
        exit(1);
    }
    return NULL == head->successor_node ? 0 : 1;
}
void initList0(ND *head)
{

    ND *temp = head->successor_node;
    head->successor_node = NULL;
    while (temp)
    {
        ND *next = temp->successor_node;
        free(temp);
        temp = next;
    }
}
void initList1(ND *head)
{
    while (head->successor_node)
    {
        ND *temp = head->successor_node;
        temp->Precursor_node->successor_node = temp->successor_node;
        if (temp->successor_node != NULL)
        {
            temp->successor_node->Precursor_node = temp->Precursor_node;
        }
        free(temp);
    }
}
void freeList(ND **head)
{

    initList0(*head);
    free(*head);
    *head = NULL;
}